package wumingya.com.studentsystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import reflash.DetailEntity;
import reflash.RecordItem;

/**
 * Created by Administrator on 2018/6/10 0010.
 */

public class DetailActivity extends Activity {
    String id = "5";
    DetailEntity stu_info;
    View myview;
    int counter = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.student);

//        myview = (View)findViewById(R.layout.student) ;
        myview = getWindow().getDecorView();
        Intent intent = getIntent();

        String student_name = intent.getStringExtra("name");
        String class_name = intent.getStringExtra("classname");
        this.id = intent.getStringExtra("id");

        System.out.println("id:" + this.id);
        this.request();
    }
    public void request() {
        //步骤4:创建Retrofit对象
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("http://10.0.2.2/stuServer/index.php/api/detail?id=" + this.id)
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback()
        {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("kwwl","获取数据失败了");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){//回调的方法执行在子线程。
                    Log.i("kwwl","获取detail数据成功了");

                    //Log.i("kwwl","response.code()=="+response.code());
                    String responseData = response.body().string();
                    System.out.println(responseData);

                    //Log.i(TAG, response.body().string());
                    parseJSONWithGSON(responseData);

//                    Detail.this.myView.post(new Runnable() {
//                        public void run() {
//                            Detail.this.showList(apk_list);
//                        }
//                    });
                }
            }
        });
    }

    private void parseJSONWithGSON(String jsonData) {
        Gson gson = new Gson();
        stu_info = gson.fromJson(jsonData, new TypeToken<DetailEntity>() {
        }.getType());

        System.out.println("pares:"+stu_info.toString());

        DetailActivity.this.myview.post(new Runnable() {
            public void run() {
                DetailActivity.this.render();
            }
        });
    }

    public void table(RecordItem record) {

        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.table);

        // create a new TableRow
        TableRow row = new TableRow(this);

        // count the counter up by one
        counter =5;

        // create a new TextView
        TextView term = new TextView(this);
        TextView course = new TextView(this);
        EditText mark = new EditText(this);
        // set the text to "text xx"

        term.setText(record.term_name);
        mark.setWidth(80);
        course.setText(record.course_name);
        mark.setWidth(80);
        mark.setText(record.mark);
        mark.setId(100+ Integer.parseInt(record.id));
        mark.setWidth(80);
        // create a CheckBox


        Button c = new Button(this);
        c.setId(Integer.parseInt(record.id));
        c.setText("修改");

        final String recordId = record.id;

        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailActivity.this.myview.post(new Runnable() {
                    public void run() {
                        DetailActivity.this.updateRecord(recordId);
                    }
                });
            }
        });

        // add the TextView and the CheckBox to the new TableRow
        row.addView(term);
        row.addView(course);
        row.addView(mark);
        row.addView(c);

        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

    }
    private void render(){
        EditText name =(EditText) findViewById(R.id.student_name) ;
        EditText classname =(EditText)findViewById(R.id.class_name);
        EditText major =(EditText) findViewById(R.id.major) ;
        EditText school =(EditText) findViewById(R.id.school) ;
        name.setText(this.stu_info.getName());
        classname.setText(stu_info.getClass_name());
        major.setText(this.stu_info.getMajor_name());
        school.setText(this.stu_info.getSchool_name());
		for (RecordItem record : this.stu_info.getRecords()) {
            this.table(record);
		}
    }

    private void updateRecord(String id) {
        EditText mark =(EditText) findViewById(100 + Integer.parseInt(id)) ;

        final String recordMark = mark.getText().toString();

                System.out.println(id);
            //步骤4:创建Retrofit对象
            //创建okHttpClient对象
            OkHttpClient mOkHttpClient = new OkHttpClient();
            //创建一个Request
            final Request request = new Request.Builder()
                    .url("http://10.0.2.2/stuServer/api/updateScore?id=" + id + "&mark=" + recordMark)
                    .build();
            //new call
            Call call = mOkHttpClient.newCall(request);
            //请求加入调度
            call.enqueue(new Callback()
            {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("kwwl","获取数据失败了");
                    DetailActivity.this.myview.post(new Runnable() {
                        public void run() {
                            DetailActivity.this.toast("成绩修改失败!");
                        }
                    });
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.isSuccessful()){//回调的方法执行在子线程。
                        Log.i("kwwl","update record 成功了");

                        //Log.i("kwwl","response.code()=="+response.code());
                        String responseData = response.body().string();
                        System.out.println(responseData);


                        //Log.i(TAG, response.body().string());
//                        parseJSONWithGSON(responseData);

                    DetailActivity.this.myview.post(new Runnable() {
                        public void run() {
                            DetailActivity.this.toast("成绩修改成功!");
                        }
                    });
                    }
                }
            });


    }

    private void toast(String msg) {
        Toast.makeText(DetailActivity.this, msg, Toast.LENGTH_LONG).show();
    }

}
